<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author PR
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span>
          {{ getTitle.value }} ({{
            ebillView.code + '-' + ebillView.name + '-' + ebillView.mainClassName
          }})</span
        >
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleExport({ billVAId: ebillView.id })">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <a-button type="default" @click="handleImport({ billVAId: ebillView.id })">
          <Icon icon="ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
        <a-button
          type="primary"
          @click="handleForm({ billVAId: ebillView.id, tableName: ebillView.mainClassName })"
          v-auth="'extd:bill:view:cont:extdBillContrast:edit'"
        >
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.tableName }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsExtdBillViewContExtdBillContrastList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    extdBillContrastDelete,
    extdBillContrastListData,
  } from '/@/api/extd/bill.view.cont/extdBillContrast';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';

  const { t } = useI18n('extd.bill.view.cont.extdBillContrast');
  const { showMessage } = useMessage();

  const ebillView = ref({
    id: ref<string>(router.currentRoute.value.query.id as string),
    code: ref<string>(router.currentRoute.value.query.code as string),
    name: ref<string>(router.currentRoute.value.query.name as string),
    mainClassName: ref<string>(router.currentRoute.value.query.mainClassName as string),
  });

  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('字段对照管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 120,
    schemas: [
      {
        label: t('系统KEY'),
        field: 'colKey',
        component: 'Input',
      },
      {
        label: t('系统名称'),
        field: 'colName',
        component: 'Input',
      },
      {
        label: t('EXTD系统KEY'),
        field: 'extdColKey',
        component: 'Input',
      },
      {
        label: t('EXTD系统名称'),
        field: 'extdColName',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('附表单据标识'),
      dataIndex: 'billVAId',
      key: 'a.bill_v_a_id',
      sorter: true,
      width: 230,
      align: 'left',
      ifShow: false,
    },
    {
      title: t('表类型标识'),
      dataIndex: 'tableName',
      key: 'a.table_name',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('系统KEY'),
      dataIndex: 'colKey',
      key: 'a.col_key',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('系统名称'),
      dataIndex: 'colName',
      key: 'a.col_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('EXTD系统KEY'),
      dataIndex: 'extdColKey',
      key: 'a.extd_col_key',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('EXTD系统名称'),
      dataIndex: 'extdColName',
      key: 'a.extd_col_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑字段对照'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'extd:bill:view:cont:extdBillContrast:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除字段对照'),
        popConfirm: {
          title: t('是否确认删除字段对照'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'extd:bill:view:cont:extdBillContrast:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: extdBillContrastListData,
    beforeFetch: (params) => {
      params.billVAId = ebillView.value.id;
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    pagination: false,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport(record: Recordable) {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url:
        ctxAdminPath +
        '/extd/bill/view/cont/extdBillContrast/exportData?billVAId=' +
        record.billVAId,
      target: '_self',
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport(record: Recordable) {
    importModal(true, { record });
  }

  async function handleDelete(record: Recordable) {
    const res = await extdBillContrastDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
